package pearson.automation.forms.courseDetails;

import java.util.List;

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pearson.automation.forms.courseDetails.AddCollaborators.AccessRights;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.ElementList;
import framework.elements.Label;
import framework.elements.Link;

/**
 * Form for collaborators management. Location:
 * CourseForm->Collaborators
 * 
 */
public class CollaboratorsForm extends BaseForm {

    private static Label identifyer = new Label(By.xpath("//h2[text()='Collaborators']"), "Collaborator settings");
    private Link makeAllPublic = new Link(By.linkText("Make all public"), "Make all public");
    private Link makeAllLocked = new Link(By.linkText("Make all locked"), "Make all locked");
    private Link addCollaborators = new Link(By.cssSelector("a[title='Add Collaborators']"), "Add Collaborators");
    private Link editCollaborators = new Link(By.cssSelector("a[title='Edit Collaborators']"), "Edit Collaborators");
    private final String xPathModuleLinkTemplate = "//div[contains(@class,'os-animated-checkbox-module-number')][contains(.,'%1$s')]";
    private final String xPathUserPermissionTemplate = "//div[@data-collaborator-ref][contains(.,'%1$s')]/span[contains(@class,'user-permissions')]";
    private Button close = new Button(By.linkText("Close"), "Close");

    public CollaboratorsForm() {
    super(identifyer.getLocator(), identifyer.getName());
    }

    public void addCollaborator(String collaboratorName, AccessRights rights) {
    addCollaborators.clickAndWait();
    new AddCollaborators().addCollaborator(collaboratorName, rights);
    checkAccessRights(collaboratorName, rights);
    }

    public void editCollaborator(String collaboratorName, AccessRights rights) {
    editCollaborators.clickAndWait();
    new EditCollaborators().changeRights(collaboratorName, rights);
    checkAccessRights(collaboratorName, rights);
    }

    public void removeCollaborator(String collaboratorName) {
    editCollaborators.clickAndWait();
    new EditCollaborators().removeCollaborator(collaboratorName);
    Label elementPermissions = new Label(By.xpath(String.format(xPathUserPermissionTemplate, collaboratorName)),
        "collaborator rights");
    elementPermissions.assertIsAbsent();
    }

    public void checkAccessRights(String collaboratorName, AccessRights rights) {
    Label elementPermissions = new Label(By.xpath(String.format(xPathUserPermissionTemplate, collaboratorName)),
        "collaborator rights");
    String permisions = elementPermissions.getText();
    switch (rights) {
    case RO:
        Assert.assertTrue(permisions.trim().equalsIgnoreCase("- read only"), "Wrong collaborator's rights");
        break;
    case RW:
        Assert.assertTrue(permisions.trim().equalsIgnoreCase("- read & write"), "Wrong collaborator's rights");
        break;
    case CREATOR:
        Assert.assertTrue(permisions.trim().equalsIgnoreCase("- Creator"), "Wrong collaborator's rights");
        break;
    }
    }

    public CourseForm clickClose() {
    close.clickAndWait();
    return new CourseForm();
    }

    public void unlockModules(String[] moduleNames) {
    info("Unlocking modules");
    for (String moduleName : moduleNames) {
        Button module = new Button(By.xpath(String.format(xPathModuleLinkTemplate, moduleName)), moduleName);
        if (!module.isChecked()) {
        module.clickAndWait();
        }
        Assert.assertTrue(module.isChecked(), "Module is unchecked");
    }
    // Checking, that all unlocking applies
    for (String moduleName : moduleNames) {
        Button module = new Button(By.xpath(String.format(xPathModuleLinkTemplate, moduleName)), moduleName);
        Assert.assertTrue(module.isChecked(), "Module is unchecked");
    }
    }

    /**
     * Lock all modules in collaboration
     */
    public void clickMakeAllLocked() {
    makeAllLocked.clickAndWait();
    ElementList modules = new ElementList(By.xpath(String.format(xPathModuleLinkTemplate, "")), "Modules list");
    List<WebElement> elem = modules.getListElements();
    for (WebElement module : elem.toArray(new WebElement[elem.size()])) {
        Assert.assertTrue(module.getAttribute("class").toLowerCase().contains("unchecked"), "Module is checked");
    }
    }
    /**
     * Unlock all modules in collaboration
     */
    public void clickMakeAllPublic() {
    makeAllPublic.clickAndWait();
    ElementList modules = new ElementList(By.xpath(String.format(xPathModuleLinkTemplate, "")), "Modules list");
    List<WebElement> elem = modules.getListElements();
    for (WebElement module : elem.toArray(new WebElement[elem.size()])) {
        Assert.assertFalse(module.getAttribute("class").toLowerCase().contains("unchecked"), "Module is unchecked");
    }
    }

    public void lockModules(String[] moduleNames) {
    info("Locking modules");
    for (String moduleName : moduleNames) {
        Button module = new Button(By.xpath(String.format(xPathModuleLinkTemplate, moduleName)), moduleName);
        if (module.isChecked()) {
        module.clickAndWait();
        }
        Assert.assertFalse(module.isChecked(), "Module is unchecked");
    }
    // Checking, that all locking applies
    for (String moduleName : moduleNames) {
        Button module = new Button(By.xpath(String.format(xPathModuleLinkTemplate, moduleName)), moduleName);
        Assert.assertFalse(module.isChecked(), "Module is unchecked");
    }

    }

    public void checkModuleStatus(String[] locked, String[] unlocked) {
    for (String moduleName : locked) {
        Button module = new Button(By.xpath(String.format(xPathModuleLinkTemplate, moduleName)), moduleName);
        Assert.assertFalse(module.isChecked(), String.format("Module %1$s is unlocked", moduleName));
    }
    for (String moduleName : unlocked) {
        Button module = new Button(By.xpath(String.format(xPathModuleLinkTemplate, moduleName)), moduleName);
        Assert.assertTrue(module.isChecked(), String.format("Module %1$s is locked", moduleName));
    }
    }

    /**
     * Checking access of Add button for collaborators
     */
    public void assertAddCollaboratorLocked() {
    Assert.assertFalse(addCollaborators.isEnabled(), "Add button is enabled");
    }

    /**
     * Checking access of Edit button for collaborators
     */
    public void asserEditCollaboratorLocked() {
    Assert.assertFalse(editCollaborators.isEnabled(), "Edit button is enabled");
    }
}
